<?php
require_once("config.php");

class GalInfo{
    var $count;
    var $items;

    function GalInfo($count, $items){
        $this->count=$count;
        $this->items=$items;
    }
}



$select="select ";
$fields = "count(*) amount from ( ";

$mainSql = "FROM melagranoru.titems_internet a ".
    " inner join melagranoru.tmatchingphotos b ".
    " on a.Article = b.linkArticle ".
    " inner join melagranoru.tconstme c ".
    " on a.Me = c.id ".
    " inner join melagranoru.tconstnames d ".
    " on d.id = a.Name
    inner join melagranoru.tindexvstavki e
on a.Article=e.linkArticle
inner join melagranoru.tconstgroupvstavki f
on e.linkgroupvstavka = f.id";

$sql = "SELECT b.FileName name, a.Article Article, a.price price ".$mainSql;

if ( $_SERVER['REQUEST_METHOD'] == "GET" ) {
    // If the ID is set, get the record with that ID
//    $extra = array_key_exists('id', $_GET) ? " WHERE id = ".$_GET['id'] : null;
//
//    array_key_exists('perPage', $_GET);
//    array_key_exists('currentPage', $_GET);



    $whereClause =" where ";

    if(array_key_exists('name', $_GET) && $_GET['name']!=''){
        $whereClause.= "d.name like '%".$_GET['name']."%' and ";
    }

    if(array_key_exists('jewels', $_GET) && $_GET['jewels']!=''){
        $arr = split(",",$_GET['jewels']);
        $str="'".implode("','", $arr)."'";

        $whereClause.= "d.name in (".$str.") and ";
    }

    if(array_key_exists('stone', $_GET) && $_GET['stone']!=''){
        $arr = split(",",$_GET['stone']);
        $str="'".implode("','", $arr)."'";

        $whereClause.= " f.`Вставка` in (".$str.") and ";
    }

    if(array_key_exists('price', $_GET) && $_GET['price']!=''){
        $arr= split("-",$_GET['price']);
        $whereClause.= "a.price > ".$arr[0]." and a.price < ".$arr[1]." and";
    }


    $whereClause.=" b.Order = 1 group by b.id";

    $sqlCount = $select.$fields.$sql.$whereClause." ) a";


    if(array_key_exists('perPage', $_GET)){
        $limit = " limit ".$_GET['perPage']*$_GET['currentPage'].",".$_GET['perPage'];
    }

    $r = mysql_query($sql.$whereClause.$limit,$link);
    $itemCount = mysql_query($sqlCount,$link);

    if ($extra) {
        // If the ID is set, fetch one record
        $content = mysql_fetch_assoc($r);
    }
    else {
        for($i = mysql_num_rows($itemCount); $i > 0 ; $i--) {
            $iCount[] = mysql_fetch_assoc($itemCount);
        }
        // Fetch all of the records if no ID is set
        for($i = mysql_num_rows($r); $i > 0 ; $i--) {
            $content[] = mysql_fetch_assoc($r);
        }
    }


    $galInfo=new GalInfo($iCount[0]['amount'],$content);
//    $dataOut = array('error'=>'Validation type', 'message'=>'Did not validate');
//    $response->body(json_encode($dataOut));

    if(sizeof($content)==0){
        echo "{\"count\":\"0\"}";
    }else{
        echo json_encode($galInfo);
    }

}
/** UPDATE or DELETE **/
else if ( array_key_exists ("_method", $_POST) ) {
    // Don't bother doing anything here. We don't really need it for the demo
}
/** CREATE **/
else {
    // Just return a model. Don't bother actually creating it and returning the new record
    $data = array( "id" => 1, "name" => "Joe Zim", "age" => 23 );
    // Encode it as JSON and ship it back
    echo json_encode($data);
}


?>